package mshop.com.schild_kotlin.adapters

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.orhanobut.logger.Logger
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.utils.ActivitysUtil
import mshop.com.schild_kotlin.utils.getMiniu

/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO 首页列表适配器
 */

class RankAdapter(var datas: ArrayList<ItemList>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        var viewItme = LayoutInflater.from(parent?.context).inflate(R.layout.item_home_view, parent, false)
        return ItemViewHold(viewItme)
    }

    override fun getItemCount() = datas.size


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        (holder as? ItemViewHold)?.onBind(datas.get(position))
    }

    class ItemViewHold(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title_tv: TextView = itemView.findViewById(R.id.title_tv)
        var time_tv: TextView = itemView.findViewById(R.id.time_tv)
        var item_img: ImageView = itemView.findViewById(R.id.item_img)
        var mContext: Context = itemView.context
        var mEntity: ItemList? = null

        init {
            itemView.setOnClickListener {
                itemView.setOnClickListener {
                    ActivitysUtil.ToVideoPlayerActivity(it.context, mEntity)
                }
            }
        }

        fun onBind(entity: ItemList) {
            mEntity = entity
            title_tv.text = entity.data.description
            time_tv.text = getMiniu(entity.data.duration * 1000)
            Glide.with(mContext)
                    .load(entity.data.cover.feed)
                    .into(item_img)
        }

    }

}